
#include "SpaceShip.h"

//Static Members
btAlignedObjectArray<class SpaceShip*> SpaceShip::m_spaceships;

SpaceShip::SpaceShip()
{
	//SpaceShip(btVector3(0,0,0));
}

SpaceShip::~SpaceShip(){}

SpaceShip::SpaceShip(const btVector3& positionOffset){
	
	m_ownerWorld = p_dynamicsWorld;
	btScalar size=0.2;
	m_shapes[BODYPART_CHASSIS] = new btBoxShape(btVector3(1*size,1*size,2*size));// btCapsuleShape(btScalar(0.15), btScalar(0.20));


	// Setup all the rigid bodies
	btTransform offset; offset.setIdentity();
	offset.setOrigin(positionOffset);

	btTransform transform;
	transform.setIdentity();
	transform.setOrigin(btVector3(btScalar(0.), btScalar(1.), btScalar(0.)));
	m_bodies[BODYPART_CHASSIS] = localCreateRigidBody(btScalar(1.), offset*transform, m_shapes[BODYPART_CHASSIS]);

	// Setup some damping on the m_bodies
	for (int i = 0; i < BODYPART_COUNT; ++i){
		m_bodies[i]->setDamping(0.05, 0.85);
		m_bodies[i]->setDeactivationTime(0.8);
		m_bodies[i]->setSleepingThresholds(1.6, 2.5);
	}
}

void SpaceShip::setObjectState(int state){
	isActive = (state==ACTIVE_STATE)?ACTIVE_STATE:IN_ACTIVE_STATE;
	pgObjectsMap.insert(make_pair(TYPEID_SPACESHIP,this));
		
	for (int i = 0; i < BODYPART_COUNT; ++i){
		p_dynamicsWorld->addRigidBody(m_bodies[i]);
	}
	for(int i = 0; i< JOINT_COUNT; i++){
		p_dynamicsWorld->addConstraint(m_joints[i], true);
	}
}

void SpaceShip::deleteAllSpaceShips(){
	for (int i=0;i<m_spaceships.size();i++){
		SpaceShip* ship = m_spaceships[i];
		delete ship;
	}
}